package day58_435;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @ClassName Solution
 * @Description TODO
 * @Author clockTown
 * @Date 2021/7/6 20:44
 * @Version 1.0
 */
public class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {

        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] < o2[0] && o1[1] < o2[1]) {
                    return -1;
                }else if (o1[0] > o2[0] && o1[1] > o2[1]) {
                    return 1;
                }
                return 0;
            }
        });


        int start = intervals[0][0];
        int end = intervals[0][1];

        int res = 0;

        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < end){
                res += 1;
            }
            end = Math.max(intervals[i][1], end);
        }
        return res;
    }
}
